
#include <malloc.h>
#include <iostream>
#include "list.h"

using namespace std;
using namespace lt;

void CSlist::initialize()
{
	if (NULL == m_pHeader)
	{
		m_pHeader = (TSlist *)malloc(sizeof(TSlist));
	}
	
	if (NULL != m_pHeader)
	{
		m_pHeader->next = NULL;
	}
}

int CSlist::count()
{
	int cnt = 0;
	TSlist *cur = m_pHeader->next;
	while (NULL != cur)
	{
		cnt++;
		cur = cur->next;
	}
	return cnt;
}

void CSlist::output()
{
	TSlist *cur = m_pHeader->next;

	while (NULL != cur)
	{
		cout << cur->data<<" ";
		cur = cur->next;
	}

	cout << endl;
}

void CSlist::push_back(ElmType val)
{
	TSlist *next = m_pHeader->next;
	TSlist *cur = m_pHeader;

	while (NULL != next)
	{
		next = next->next;
		cur = cur->next;
	}

	TSlist *list = (TSlist *)malloc(sizeof(TSlist));
	if (NULL == list)
	{
		return;
	}

	cur->next = list;
	list->data = val;
	list->next = NULL;
}

void CSlist::push_front(ElmType val)
{
	TSlist *list = (TSlist *)malloc(sizeof(TSlist));
	if (NULL == list)
	{
		return;
	}

	list->data = val;
	list->next = m_pHeader->next;
	m_pHeader->next = list;
}

void CSlist::insert(int pos, ElmType val)
{
	if ((pos <=0 ) || (pos > count()))
	{
		return;
	}

	TSlist *cur = m_pHeader->next;
	while ((NULL != cur) && (--pos != 0))
	{
		cur = cur->next;
	}

	TSlist *list = (TSlist *)malloc(sizeof(TSlist));
	if (NULL == list)
	{
		return;
	}

	list->data = val;
	list->next = cur->next;
	cur->next = list;
}

void CSlist::remove(int pos)
{

}











